﻿/* Date Units */
var _DateUnits = new Object();
    _DateUnits.Day   = "day";
    _DateUnits.Month = "month";
    _DateUnits.Year  = "year";

var DateTimeHelper = new Object();
/* Methods */
    DateTimeHelper.CreateDate           = _createDate;
    DateTimeHelper.CreateDateFromDate   = _createDateFromDate;
    DateTimeHelper.AddUnits             = _addUnits;
    DateTimeHelper.ConvertToDate        = _convertToDate;
    DateTimeHelper.FormatDate           = _formatDate;
    DateTimeHelper.GetCurrentDate       = _getCurrentDate;
    DateTimeHelper.GetCurrentTime       = _getCurrentTime;
    DateTimeHelper.GetDateParts         = _getDateParts;
    DateTimeHelper.GetDay               = _getDay;
    DateTimeHelper.GetMonth             = _getMonth;
    DateTimeHelper.GetYear              = _getYear;
/* Enums */    
    DateTimeHelper.DateUnits            = _DateUnits;

/* Dates */
function _createDateFromDate(date)
{
    return _createDate( date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds() );
}
function _createDate(year, month, day, hours, minutes, seconds, milliseconds)
{
    return new Date(year, month, day, hours != null ? hours : 0, minutes != null ? minutes : 0, seconds != null ? seconds : 0, milliseconds != null ? milliseconds : 0);
}
function _addUnits(date, unit, amount)
{
    switch ( unit )
    {
        case _DateUnits.Day:
            date.setDate( date.getDate() + amount );
            break;
        case _DateUnits.Month:
            if ( amount >= 0 )
                date.setMonth( date.getMonth() + amount ); /* date.setMonth( (date.getMonth() +1) + amount ); */
            else
                date.setMonth( date.getMonth() + amount );
            break;
        case _DateUnits.Year:
            date.setFullYear( date.getFullYear() + amount );
            break;
    }
    return date;
}
function _convertToDate(date, yearPos, monthPos, dayPos, separator)
{
    if (separator == null)
        separator = "-";
    if (yearPos == null)
        yearPos = 1;
    if (monthPos == null)
        monthPos = 2;
    if (dayPos == null)
        dayPos = 3;
    
    /* Example: "2013-04-17T03:00:00" */
    var arrDateParts = date.split(separator);
    var year         = parseInt(arrDateParts[yearPos - 1]);
    var month        = parseInt(arrDateParts[monthPos - 1]);
    var day          = parseInt(arrDateParts[dayPos - 1]);

    return new Date(year, month - 1, day);
}
function _formatDate(date, format)
{
    var defaultFormat = 'yy-mm-dd';
    if ( !isEmpty(format) )
        defaultFormat = format;

    if ( date instanceof Date ) // was created by => new Date();
        date = date;
    else
        date = _convertToDate(date);
    
    return $.datepicker.formatDate(defaultFormat, date);
}
function _getCurrentDate()
{
    return new Date();
}
function _getCurrentTime()
{
    var currentDate = new Date();
    return currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds();
}
function _getDay(date)
{
    return date.getDay();
}
function _getMonth(date)
{
    return date.getMonth() +1;
}
function _getYear(date)
{
    return date.getFullYear();
}
function _getDateParts(date)
{
    var year    = date.getFullYear();
    var month   = date.getMonth() +1;
    var day     = date.getDate();

    var dateInfo = new Object();
        dateInfo.Year   = year;
        dateInfo.Month  = month;
        dateInfo.Day    = day;

    return dateInfo;
}
function _parseJavascriptDate(date, format)
{
    var defaultFormat = 'yy-mm-dd';
    if ( !isEmpty(format) )
        defaultFormat = format;
    return $.datepicker.formatDate(defaultFormat, date);
}